<?php

namespace Modules\System\Http\Controllers\Admin;

use Illuminate\Http\Request;

class Form extends Base
{
    /**
     * 依赖注入
     */
    public function __construct(){

        //自行配制
        $this->models = new \Modules\Chinadmin\Models\FormModel();
        parent::__construct();

    }

    public function getModels($i){

        //只用id,就是玩，为了复杂而麻烦
        $sys_form = [16,17,18,19,20,21,22,23,81,82,83,84,85,86,87];

        //先写死
        if(in_array($i['id'],$sys_form)){
            $this->models = new \Modules\System\Models\FormModel();
        }else{
            $this->models = new \Modules\Chinadmin\Models\FormModel();
            $this->isSysModels = 1;
            $this->Modules = "Chinadmin";
        }

    }

    /**
     * 添加页:展示一个字段
     */
    public function showOneField_create(Request $request){

        $i = $request->input();
        $this->getModels($i);
        unset($i['MT']);
        $i['showOneField'] = 1;  //只取一条

        $data = $this->createData($request,$i);

        unset($data['this_data']);
        $data['this_data'][$data['form'][$i['id']]['field']] = '';              //只留一个数据展示

        return view('chinadmin::admin.' . $this->class . '.example_create',$data);

    }

    /**
     * 列表页:展示一个字段
     */
    public function showOneField_index(Request $request){

        $i = $request->input();
        $this->getModels($i);
        unset($i['MT']);
        $i['showOneField'] = 1;                 //只取一条

        $data = $this->indexData($i,$request);

        return view('chinadmin::admin.' . $this->class . '.example_index',$data);

    }

    /**
     * 更新
     *
     * http://l.com/chinadmin/form/update
     */
    public function update(Request $request){

        $i = $request->input();

        if(empty($i['table'])){
            mdsResponse(400,"必须有表名!");
        }

        $data['width'] = $i['td_width'];
        $where['field'] = $i['data_field'];
        $where['table_name'] = $i['table'];

        $re = \Modules\Chinadmin\Models\FormModel::where($where)->update($data);

        mds_if_response($re);

    }

}
